package com.hjwoo.app.views;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.been.OrdersObject;
import com.hjwoo.app.utils.LoanLog;
import com.hjwoo.app.utils.Utils;

import java.util.TimeZone;

/**
 * Created by yangchuan on 2016/5/12.
 * 订单详情
 */
public class OrderTrackingActivity extends BaseAppCompatActivity implements View.OnClickListener
{
    private TextView ordertnumber, address, habitableroom, orientation, area, type, factor, submittime,
        loanagreement, loanmoney, loandeadline, mortgagetype, houseimage, borrowername, idcardnumber,
        borrowermarriage, pledgername, protecttime, pledgeidcard, pledgemarriage;
    private Button pointbtn, ordertringbtn, callphone;
    private LinearLayout relative, pledgervisibility;

    private OrdersObject ordersObject;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ordertracking);
        init();

    }

    private void init()
    {

        Utils.initTitle("订单详情", OrderTrackingActivity.this, true);

        ordertnumber = (TextView) findViewById(R.id.ordertracking_ordernumber);//订单编号
        pointbtn = (Button) findViewById(R.id.ordertracking_pointbtn);//已返点标示
        relative = (LinearLayout) findViewById(R.id.ordertracking_linear);//订单跟踪

        //条目一
        address = (TextView) findViewById(R.id.ordertracking_address);//房产地址
        habitableroom = (TextView) findViewById(R.id.ordertracking_habitableroom);//居室(三居室)
        orientation = (TextView) findViewById(R.id.ordertracking_orientation);//朝向
        area = (TextView) findViewById(R.id.ordertracking_area);//建筑面积
        type = (TextView) findViewById(R.id.ordertracking_type);//物业类型(住宅)
        factor = (TextView) findViewById(R.id.ordertracking_factor);//特殊因素(临街)
        submittime = (TextView) findViewById(R.id.ordertracking_submit_time);//提交时间


        //条目二
        loanagreement = (TextView) findViewById(R.id.ordertracking_loanagreement);//抵押一致
        loanmoney = (TextView) findViewById(R.id.ordertracking_loanmoney);//贷款金额
        loandeadline = (TextView) findViewById(R.id.ordertracking_loandeadline);//贷款期限
        mortgagetype = (TextView) findViewById(R.id.ordertracking_mortgagetype);//抵押类型
        houseimage = (TextView) findViewById(R.id.ordertracking_houseimage);//房产照片
        borrowername = (TextView) findViewById(R.id.ordertracking_borrowername);//借款人姓名
        idcardnumber = (TextView) findViewById(R.id.ordertracking_idcardnumber);//借款人身份证号码
        borrowermarriage = (TextView) findViewById(R.id.ordertracking_borrowermarriage);//借款人婚姻

        //显示隐藏判断，是否有抵押人
        pledgervisibility = (LinearLayout) findViewById(R.id.ordertracking_pledger_visibility);//是否有抵押人
        pledgername = (TextView) findViewById(R.id.ordertracking_pledgername);//抵押人姓名
        pledgeidcard = (TextView) findViewById(R.id.ordertracking_pledgeidcard);//抵押人身份证
        pledgemarriage = (TextView) findViewById(R.id.ordertracking_pledgemarriage);//抵押人婚姻状况


        protecttime = (TextView) findViewById(R.id.ordertracking_protecttime);//保护期时长

        ordertringbtn = (Button) findViewById(R.id.ordertracking_btn);//订单跟踪按钮
        callphone = (Button) findViewById(R.id.ordertracking_callphone);//电话拨打按钮
        relative.setOnClickListener(this);
        ordertringbtn.setOnClickListener(this);
        callphone.setOnClickListener(this);
        if (getIntent() != null)
        {
            ordersObject = (OrdersObject) getIntent().getSerializableExtra("order");
            if (ordersObject.getStage().getCode().equals("01") || ordersObject.getStage().getCode().equals("02"))
            {
                findViewById(R.id.ordertacking_ll2).setVisibility(View.GONE);
            }

            try {
                initview();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    private void initview() throws Exception {
        ordertnumber.setText(ordersObject.getSerialNumber());
        if (ordersObject.getStage().getName().equals("评房已完成"))
        {
            pointbtn.setText("已评房");
        }else if (ordersObject.getStage().getName().equals("报单申请已提交") || ordersObject.getStage().getName().equals("基础材料已提供")
                || ordersObject.getStage().getName().equals("征信查询已完成")|| ordersObject.getStage().getName().equals("信息已完善"))
        {
            pointbtn.setText("已报单");
        }
        else if (ordersObject.getStage().getName().equals("房产初审已完成") ||ordersObject.getStage().getName().equals("产调已完成"))
        {
            pointbtn.setText("已初审");
        }

//        else if( ordersObject.getStage().getName().equals("预约申请已提交") || ordersObject.getStage().getName().equals("预约已指派") || ordersObject.getStage().getName().equals("产调已完成") || ordersObject.getStage().getName().equals("征信查询已完成") || ordersObject.getStage().getName().equals("下户已完成")){
//            pointbtn.setText("已预约");
//        }

        else if (ordersObject.getStage().getName().equals("审批已完成") || ordersObject.getStage().getName().equals("抵押公证手续已完成") ||
                ordersObject.getStage().getName().equals("放款已完成"))
        {
            String str = ordersObject.getInterestPaymentMethod() == null ? "暂无付息方式" : ordersObject.getInterestPaymentMethod().getName();
            pointbtn.setText("已审批");
        }
        else if (ordersObject.getStage().getName().equals("返点已完成"))
        {
            pointbtn.setText("已返点");
        }

        if (ordersObject.getStatus().equals("Failed"))
        {
            pointbtn.setText("已失败");
        }

        if (ordersObject.getStatus().equals("Failed"))
        {
            pointbtn.setText("已失败");
        }

        address.setText(ordersObject.getDistrict());
        habitableroom.setText(ordersObject.getNumberOfLivingRoom()+ "居室");
        orientation.setText(ordersObject.getOrientation());
        area.setText(ordersObject.getArea() + "㎡");
        if (ordersObject.getHouseType()!= null){
            type.setText(ordersObject.getHouseType().getName());
        }

        if (ordersObject.getSpecialFactors() != null){
            factor.setText(ordersObject.getSpecialFactors().getName());
        }

        String str = ordersObject.getCreatedDate();

//        2016-07-18T13:24:14Z

        submittime.setText(Utils.converTime(str, TimeZone.getTimeZone("GMT+8")));
//        submittime.setText(Utils.getLocalTimeFromUTC(str));
        loanagreement.setText("是");
        Log.e("contacts", ordersObject.getContacts().toString());
        for (int i = 0; i < ordersObject.getContacts().size(); i++)
        {
            if (ordersObject.getContacts().get(i).type.getName().equals("抵押人"))
            {
                loanagreement.setText("否");
                Log.e("getContacts()", "loanagreement.setText(\"否\");");
                break;
            }
        }
        Log.e("getContacts().size()", ordersObject.getContacts().size() + "");
        loanmoney.setText(ordersObject.getRequestedAmount()+ "万元");
        loandeadline.setText(ordersObject.getLoanDuration() + "月");//贷款期限
        if (ordersObject.getMortgageType()!=null){
            mortgagetype.setText(ordersObject.getMortgageType().getName());
        }
        houseimage.setText("已上传");

//        //显示隐藏判断，是否有抵押人
        pledgervisibility = (LinearLayout) findViewById(R.id.ordertracking_pledger_visibility);//是否有抵押人
        pledgername = (TextView) findViewById(R.id.ordertracking_pledgername);//抵押人姓名
        pledgeidcard = (TextView) findViewById(R.id.ordertracking_pledgeidcard);//抵押人身份证
        pledgemarriage = (TextView) findViewById(R.id.ordertracking_pledgemarriage);//抵押人婚姻状况

        borrowername.setText(ordersObject.getFullName());//借款人姓名
        idcardnumber.setText(ordersObject.getIdNumber());//借款人身份证号码
        borrowermarriage.setText(ordersObject.getMaritalStatus());

        LoanLog.e("loan", ordersObject.toString());


        pledgemarriage.setText("未婚");


        for (int i = 0; i < ordersObject.getContacts().size(); i++)
        {
            if (ordersObject.getContacts().get(i).getType().getName().equals("抵押人"))
            {
//                有抵押人设置为显示
                pledgervisibility.setVisibility(View.VISIBLE);
                pledgername.setText(ordersObject.getContacts().get(i).getContact().getFullName());
                pledgeidcard.setText(ordersObject.getContacts().get(i).getContact().getIdNumber());

                pledgemarriage.setText(ordersObject.getContacts().get(i).getContact().getMaritalStatus());
            }
        }
//        "protectionEndTime": "2016-06-01T10:17:26Z",
//        "protectionStartTime": "2016-05-17T10:17:26Z",
//        .replace("T"," ").replace("Z","")
        if (ordersObject.getProtectionStartTime()!= null || ordersObject.getProtectionEndTime() != null)
        {
            protecttime.setText(String.valueOf(Utils.daysBetween(ordersObject.getProtectionStartTime().replace(" T", " ").replace("Z", ""), ordersObject.getProtectionEndTime().replace("T", " ").replace("Z", ""))) + "天（最多15天）");
        }
        else
        {
            protecttime.setText("暂无");
        }
    }

    @Override
    public void onClick(View view)
    {
        Intent mIntent = new Intent(OrderTrackingActivity.this, OrderStateActivity.class);
        Bundle mBundle = new Bundle();
        mBundle.putSerializable("order", ordersObject);
        mBundle.putString("stage", pointbtn.getText().toString());
        mIntent.putExtras(mBundle);
        switch (view.getId())
        {
        case R.id.ordertracking_linear: //title订单跟踪点击事件
            startActivity(mIntent);
            break;
        case R.id.ordertracking_btn://底部订单按钮点击事件
            startActivity(mIntent);
            break;
        case R.id.ordertracking_callphone://拨打电话点击事件
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_CALL);
            //url:统一资源定位符
            //uri:统一资源标示符（更广）
            intent.setData(Uri.parse("tel:" + callphone.getText()));
            //开启系统拨号器
            startActivity(intent);
            break;
        }
    }
}
